package net.sunxiaolong.tool.captureinsureknowledge.controller;

import net.sunxiaolong.tool.captureinsureknowledge.entity.CpicKnowledge;
import net.sunxiaolong.tool.captureinsureknowledge.repository.CpicKnowledgeRepository;
import net.sunxiaolong.tool.captureinsureknowledge.tool.NetworkTool;
import net.sunxiaolong.tool.captureinsureknowledge.tool.capture.CpicCapture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.htmlcleaner.ContentNode;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 晓龙
 * @create 2018-08-07 下午4:14
 * @desc
 **/
@Controller
@RequestMapping(value = "cpic")
public class CpicCaptureController {

    @Autowired
    private CpicKnowledgeRepository knowledgeRepository;

    @RequestMapping("cpicCapture")
    @ResponseBody
    public String cpicCapture(@RequestParam(required = true) String basePathUrlStr) throws XPatherException, InterruptedException {
        HtmlCleaner hc = new HtmlCleaner();
//        String basePathUrlStr = "http://www.cpic.com.cn/zixun2/rsbxzs/";
        String thisPageUrlStr = basePathUrlStr + "index.shtml";
        String thisPageContent = NetworkTool.getStringFromUrl(thisPageUrlStr, null);
        TagNode thisPageTn = hc.clean(thisPageContent);
        List pageNumberNodeList = ((TagNode) thisPageTn.evaluateXPath("//input[@class='z_num']/..")[0]).getAllChildren();
        ContentNode pageNumbeCn = (ContentNode) pageNumberNodeList.get(pageNumberNodeList.size() - 2);
        int pageCount = Integer.parseInt(pageNumbeCn.getContent().split("/")[1].replaceAll("页", "").replaceAll("&nbsp;", ""));
        int forExecuteNumber = pageCount % 100 == 0 ? pageCount / 100 : pageCount / 100 + 1;
        ExecutorService executor = Executors.newCachedThreadPool();
        CountDownLatch doneSignal = new CountDownLatch(forExecuteNumber);
        for (int i = 0; i < forExecuteNumber; i++) {
            int startPage = i * 100 + 1;
            int endPage = startPage + 99 > pageCount ? pageCount : startPage + 99;
            CpicCapture thisCapture = new CpicCapture(doneSignal, knowledgeRepository, startPage, endPage, basePathUrlStr);
            executor.submit(thisCapture);
            System.out.println("startPage:" + startPage + "\tendPage:" + endPage);
        }
        doneSignal.await();
        return "OK";
    }


    @RequestMapping(value = "exp2ExcelCpic")
    @ResponseBody
    public String exp2ExcelSingleThreadCpic() throws IOException {
        System.out.println("startTime:" + LocalDateTime.now().toLocalTime());
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet cpicSheet = workbook.createSheet("CPIC知识库");
        int thisSheetRowIndex = 0;
        Row headRow = cpicSheet.createRow(thisSheetRowIndex++);
        int thisRowCellIndex = 0;
        headRow.createCell(thisRowCellIndex++).setCellValue("DBID");
        headRow.createCell(thisRowCellIndex++).setCellValue("标题");
        headRow.createCell(thisRowCellIndex++).setCellValue("内容");
        headRow.createCell(thisRowCellIndex++).setCellValue("标签");
        headRow.createCell(thisRowCellIndex++).setCellValue("链接");
        headRow.createCell(thisRowCellIndex++).setCellValue("根链接");
        List<CpicKnowledge> knowledgeList = knowledgeRepository.getAllData();
        int size = knowledgeList.size();
        for (int i = 0; i < size; i++) {
            CpicKnowledge knowledge = knowledgeList.get(i);
            Row thisRow = cpicSheet.createRow(i + 1);
            thisRowCellIndex = 0;
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getId());
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getTitle());
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getContent());
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getTag());
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getHref());
            thisRow.createCell(thisRowCellIndex++).setCellValue(knowledge.getBaseHref());
        }

        FileOutputStream fos = new FileOutputStream("/tmp/CPIC保险知识库" + System.currentTimeMillis() + ".xlsx");
        workbook.write(fos);
        fos.flush();
        workbook.close();
        fos.close();

        System.out.println("endTime:" + LocalDateTime.now().toLocalTime());
        return "single thread export finish";
    }


}
